<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Registers a User Defined Function for use in SQL statements</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.sqlitecreateaggregate.html">PDO::sqliteCreateAggregate</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.pdo-4d.html">4D (PDO)</a></div>
 <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo.sqlitecreatefunction" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::sqliteCreateFunction</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo_sqlite &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">PDO::sqliteCreateFunction</span> &mdash; <span class="dc-title">
   Registers a User Defined Function for use in SQL statements
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdo.sqlitecreatefunction-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>PDO::sqliteCreateFunction</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$function_name</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$num_args</code></span>
  ] )</div>

  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">此函数是<em class="emphasis">实验性</em>的。此函数的表象，包括名称及其相关文档都可能在未来的
PHP 发布版本中未通知就被修改。使用本函数风险自担 。</p></div>

  <p class="para">
   This method allows you to register a PHP function with SQLite as an 
   <acronym title="User Defined Functions">UDF</acronym> (User Defined Function), so that it can be called
   from within your SQL statements.
  </p>
  <p class="para">
   The UDF can be used in any SQL statement that can call functions, such as
   SELECT and UPDATE statements and also in triggers.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdo.sqlitecreatefunction-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">function_name</code></em></dt>

     <dd>

      <p class="para">
       The name of the function used in SQL statements.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">callback</code></em></dt>

     <dd>

      <p class="para">
       Callback function to handle the defined SQL function.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        Callback functions should return a type understood by SQLite (i.e.
        <a href="language.types.intro.html" class="link">scalar type</a>).
       </span>
      </p></blockquote>
     </dd>

    
    
     <dt>
<em><code class="parameter">num_args</code></em></dt>

     <dd>

      <p class="para">
       Hint to the SQLite parser if the callback function accepts a
       predetermined number of arguments.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.sqlitecreatefunction-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.sqlitecreatefunction-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-965">
    <p><strong>Example #1 <span class="function"><strong>PDO::sqliteCreateFunction()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">md5_and_reverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite:sqlitedb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sqliteCreateFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'md5rev'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'md5_and_reverse'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;md5rev(filename)&nbsp;FROM&nbsp;files'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   In this example, we have a function that calculates the md5 sum of a
   string, and then reverses it.  When the SQL statement executes, it
   returns the value of the filename transformed by our function.  The data
   returned in <em><code class="parameter">$rows</code></em> contains the processed result.
  </p>
  <p class="para">
   The beauty of this technique is that you do not need to process the
   result using a <a href="control-structures.foreach.html" class="link">foreach</a> loop after you have queried for the data.
  </p>
  
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    You can use <a href="pdo.sqlitecreatefunction.html" class="xref">PDO::sqliteCreateFunction</a> and
    <a href="pdo.sqlitecreateaggregate.html" class="xref">PDO::sqliteCreateAggregate</a> to override SQLite
    native SQL functions.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This method is not available with the SQLite2 driver.
    Use the old style sqlite API for that instead.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo.sqlitecreatefunction-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pdo.sqlitecreateaggregate.html" class="xref">PDO::sqliteCreateAggregate</a></li>
    <li class="member"><span class="function"><a href="function.sqlite-create-function.html" class="function" rel="rdfs-seeAlso">sqlite_create_function()</a> - Registers a &quot;regular&quot; User Defined Function for use in SQL statements</span></li>
    <li class="member"><span class="function"><a href="function.sqlite-create-aggregate.html" class="function" rel="rdfs-seeAlso">sqlite_create_aggregate()</a> - Register an aggregating UDF for use in SQL statements</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.sqlitecreateaggregate.html">PDO::sqliteCreateAggregate</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.pdo-4d.html">4D (PDO)</a></div>
 <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
